package com.hethongviet.job.helpers;

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageLoadTask extends AsyncTask<String, Void, Bitmap>
{
	private Activity	activity;
	private ImageView	imageView;
	//private FlickrPhoto	flickrPhoto;

	public ImageLoadTask(Activity activity, ImageView imageView
			//, FlickrPhoto flickrPhoto
			)
	{
		this.activity = activity;
		this.imageView = imageView;
		//this.flickrPhoto = flickrPhoto;
	}

	protected Bitmap doInBackground(String... url)
	{
		// ---download an image---
		Bitmap bitmap = downloadImage(url[0]);
		try
		{
			Thread.sleep(3000);
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		return bitmap;
	}

	protected void onPostExecute(Bitmap bitmap)
	{
		imageView.setImageBitmap(bitmap);
		//flickrPhoto.setBitmap(bitmap);
	}

	public Bitmap downloadImage(String URL)
	{
		Bitmap bitmap = null;
		InputStream inputStream = null;
		try
		{
			inputStream = new OpenHttpConnection().get(URL);
			bitmap = BitmapFactory.decodeStream(inputStream);
			if (inputStream != null) inputStream.close();
		} catch (IOException e1)
		{
			Toast.makeText(activity, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();
			e1.printStackTrace();
		}
		return bitmap;
	}
}
